﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;

namespace ERP_RORZE_PRODUCT.Product.Quotation
{
    public partial class frm_QmachineEdit : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler Reload;
        ClsQuationDAO clsQuotationDAO = new ClsQuationDAO();
        ClsPartDataDAO clsPartDataDAO = new ClsPartDataDAO();
        ClsMachineDAO clsMachineDAO = new ClsMachineDAO();
        DataTable dt;
        private LogUtils log;
        Qmachine b = new Qmachine();

        public string RecordNo;
        public string InputDate;
        public string QuotationID;
        public string StaffID;
        public string MPartNo;
        public string MachineID;
        public string OptionID;
        public string ProTime;
        public string ClampTime;
        public string Note;

        public frm_QmachineEdit()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_QmachineEdit_Load(object sender, EventArgs e)
        {
            dt = clsQuotationDAO.GetAllQuotation();
            cbQuotationID.DataSource = dt;
            cbQuotationID.DisplayMember = "QuotationID";
            cbQuotationID.ValueMember = "QuotationID";

            //
            dt = clsQuotationDAO.GetAllStaff();
            cbStaffID.DataSource = dt;
            cbStaffID.DisplayMember = "StaffID";
            cbStaffID.ValueMember = "StaffID";

            //
            dt = clsPartDataDAO.GetAllPartID();
            cbMPartNo.DataSource = dt;
            cbMPartNo.DisplayMember = "PartNo";
            cbMPartNo.ValueMember = "PartNo";

            //
            dt = clsMachineDAO.GetAllMachine();
            cbMachineID.DataSource = dt;
            cbMachineID.DisplayMember = "MachineID";
            cbOptionID.ValueMember = "MachineID";

            //
            dt = clsPartDataDAO.GetAllOptionID();
            cbOptionID.DataSource = dt;
            cbOptionID.DisplayMember = "OptionID";
            cbOptionID.ValueMember = "OptionID";

            //
            txtClampTime.Text = ClampTime;
            txtNote.Text = Note;
            txtProTime.Text = ProTime;
            cbMachineID.Text = MachineID;
            cbMPartNo.Text = MPartNo;
            cbOptionID.Text = OptionID;
            cbQuotationID.Text = QuotationID;
            cbStaffID.Text = StaffID;
            dateInputDate.Value = DateTime.Parse(InputDate);

        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtClampTime.Text == "")
            {
                MessageBox.Show("Chưa nhập ClampTime");
            }
            else if (txtProTime.Text == "")
            {
                MessageBox.Show("Chưa nhập ProTime");
            }
            else
            {
                try
                {
                    float.Parse(txtClampTime.Text);
                    try
                    {
                        float.Parse(txtProTime.Text);
                        b.RecordNo = int.Parse(RecordNo);
                        b.ClampTime = float.Parse(txtClampTime.Text);
                        b.InputDate = dateInputDate.Value;
                        b.MachineID = cbMachineID.Text;
                        b.MPartNo = cbMPartNo.Text;
                        b.Note = txtNote.Text;
                        b.OptionID = cbOptionID.Text;
                        b.ProTime = float.Parse(txtProTime.Text);
                        b.QuotationID = cbQuotationID.Text;
                        b.StaffID = cbStaffID.Text;
                        clsQuotationDAO.DeleteQuotationMachine(b);

                        //write Log
                        log.Table = LogConstant.Table.Product.QuotationMachine;
                        log.Task = LogConstant.Task.Update;
                        log.Description = "QuotationID: " + b.QuotationID + "; InputDate: " + b.InputDate + "; MPartNo: " + b.MPartNo;
                        log.WriteLog();

                        Reload(sender, e);
                    }
                    catch
                    {
                        MessageBox.Show("ProTime phải là kiểu số thực");
                    }
                }
                catch
                {
                    MessageBox.Show("ClampTime phải là kiểu số thực");
                }
            }
        }
    }
}